import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/exam/widgets/BGExamWidget.dart';
import '../../controller/assessments.dart';
import '../../exam/dto/assessment.response.dart';
import '../../widgets/buttons/debouncedMaterialButton.dart';

class SwitchEdit extends StatefulWidget {
  SwitchEdit(
      {Key? key,
      required this.questionItem,
      this.partType,
      required this.questionId,
      required this.nanhaiBookKnowledgeTypes,
      this.getAnswer,
      required this.mode,
      required this.isTeacherOrSchoolAdmin,
      required this.recordStatus,
      this.saveAnswers})
      : super(key: key);
  final QuestionItem questionItem;
  final String? partType;
  final String questionId;
  final NanhaiBookKnowledgeTypes nanhaiBookKnowledgeTypes;
  final BGExamWidgetMode mode;
  final Function(String)? getAnswer;
  final bool isTeacherOrSchoolAdmin;
  final String recordStatus;
  final Function()? saveAnswers;
  @override
  State<SwitchEdit> createState() => _SwitchEdit();
}

class _SwitchEdit extends State<SwitchEdit> {
  final AssessmentsController _assessmentsController = Get.find();
  bool switchSave = false;
  String writtenString = '';
  List<Map> writtenJson = [];
  @override
  void initState() {
    // set the grade and comment if it is graded
    if (widget.recordStatus == 'COMPLETED' &&
        widget.nanhaiBookKnowledgeTypes ==
            NanhaiBookKnowledgeTypes.quickReview) {
      writtenString = _assessmentsController
              .assessmentRecordsGrades.value.examLibRecord?.writtenJson ??
          '';
      if (writtenString != '') {
        writtenJson = json.decode(writtenString).cast<Map<dynamic, dynamic>>();
        writtenJson.forEach((element) {
          if (element['questionId'] == widget.questionId) {
            widget.questionItem.score = element['score'];
            widget.questionItem.comment = element['comment'];
          }
        });
      }
    }
    super.initState();
  }

  @override
  void dispose() {
    Get.delete<SwitchEdit>();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        BGExamWidget(
            questionItem: widget.questionItem,
            getAnswer: widget.getAnswer,
            questionId: widget.questionId,
            mode: widget.mode,
            switchSave: switchSave),
        SizedBox(height: 15),
        Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            Visibility(
                visible: widget.isTeacherOrSchoolAdmin == false &&
                    widget.recordStatus != 'COMPLETED',
                child: DebouncedMaterialButton(
                  debounceTimeMs: 800,
                  color: Theme.of(context).colorScheme.secondary,
                  onPressed: () {
                    if (switchSave == false) {
                      setState(() {
                        switchSave = true;
                      });
                    } else {
                      widget.saveAnswers!();
                    }
                  },
                  child: this.switchSave
                      ? Text('Save'.tr, style: TextStyle(color: Colors.white))
                      : Text('Edit'.tr, style: TextStyle(color: Colors.white)),
                )),
          ],
        )
      ],
    );
  }
}
